package map;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

/*
    Map支持三种遍历方式:
    1.遍历所有的key
    2.遍历所有的键值对
    3.遍历所有的value(相对不常用)
 */
public class MapDemo2 {
    public static void main(String[] args) {
        Map<String,Integer> map = new HashMap<>();
        map.put("物理",96);
        map.put("数学",98);
        map.put("语文",99);
        map.put("化学",99);
        map.put("英语",97);
        System.out.println(map);

        //遍历所有的key
        Set<String> keySet = map.keySet();
        for(String key : keySet){
            System.out.println("key:"+key);
        }
        System.out.println("___________________________");
        //遍历所有键值对
        Set<Map.Entry<String,Integer>> entrySet = map.entrySet();
        for(Map.Entry<String,Integer> e : entrySet){
            String key = e.getKey();
            Integer value = e.getValue();
            System.out.println("key:"+key+",value:"+value);
        }
        System.out.println("_______________________________");
        //遍历所有的value
        Collection<Integer> values = map.values();
        for(Integer value : values){
            System.out.println("value:"+value);
        }

        values.forEach(System.out::println);//集合forEach()

        map.forEach((k,v)->{System.out.println(k+":"+v);});//Map的forEach()

    }
}
